package cn.edu.xmu.oomall.customer.controller;

import cn.edu.xmu.javaee.core.aop.Audit;
import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.Constants;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.javaee.core.model.vo.PageVo;
import cn.edu.xmu.oomall.customer.controller.dto.UserQueryDto;
import cn.edu.xmu.oomall.customer.controller.vo.CustomerVo;
import cn.edu.xmu.oomall.customer.dao.bo.Customer;
import cn.edu.xmu.oomall.customer.service.CustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import cn.edu.xmu.javaee.core.util.CloneFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 谢传艳
 * @date 2024/12/18
 */
@RestController
@RequestMapping(value = "/shops/{shopId}", produces = "application/json;charset=UTF-8")
@RequiredArgsConstructor
@Transactional
public class ShopController {

    private final CustomerService customerService;

    /**
     * 管理员查询所有用户信息
     * @param shopId 商店id
     * @param username 用户名
     * @param name 姓名
     * @param mobile 手机号
     * @param page 页码
     * @param pageSize 每页大小
     * @return ReturnObject
     */
    @Audit(departName = "shops")
    @GetMapping("/customers")
    public ReturnObject getAllUserInfo(@PathVariable Long shopId,
                                       @RequestParam(required = false) String username,
                                        @RequestParam(required = false) String name,
                                       @RequestParam(required = false) String mobile,
                                       @RequestParam(required = false, defaultValue = "1") Integer page,
                                       @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        if(!Constants.PLATFORM.equals(shopId)){
            throw  new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE);
        }
        UserQueryDto userQueryDto = new UserQueryDto(username, name, mobile);
        List<Customer> customerList = customerService.getAllUserInfo(userQueryDto, page, pageSize);
        return new ReturnObject(ReturnNo.OK, new PageVo<>(customerList.stream().map(o -> CloneFactory.copy(new CustomerVo(o), o)).toList(), page, pageSize));
    }


    /**
     * 管理员根据id查询用户信息
     * @param shopId 商店id
     * @param id 用户id
     * @return ReturnObject
     */
    @Audit(departName = "shops")
    @GetMapping("/customers/{id}")
    public ReturnObject getUserById(@PathVariable Long shopId,
                                    @PathVariable Long id){
        if(!Constants.PLATFORM.equals(shopId)){
            throw  new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE, String.format(ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage(),"顾客", id, shopId));
        }
        Customer customer = customerService.getUserById(id);
        return new ReturnObject(ReturnNo.OK, CloneFactory.copy(new CustomerVo(customer), customer));
    }

    /**
     * 管理员删除用户
     * @param shopId 商店id
     * @param id 用户id
     * @param user 用户
     */
    @Audit(departName = "shops")
    @DeleteMapping("/customers/{id}")
    public ReturnObject delUserById(@PathVariable Long shopId,
                                    @PathVariable Long id,
                                    @LoginUser UserDto user){
        if(!Constants.PLATFORM.equals(shopId)){
            throw  new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE, String.format(ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage(),"顾客", id, shopId));
        }
        customerService.delUserById(id, user);
        return new ReturnObject(ReturnNo.OK);
    }

    /**
     * 管理员封禁用户
     * @param shopId 商店id
     * @param id 用户id
     * @param user 用户
     */
    @Audit(departName = "shops")
    @PutMapping("/customers/{id}/ban")
    public ReturnObject banUser(@PathVariable Long shopId,
                                @PathVariable Long id,
                                @LoginUser UserDto user){
        if(!Constants.PLATFORM.equals(shopId)){
            throw  new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE, String.format(ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage(),"顾客", id, shopId));
        }
        customerService.banUser(id, user);
        return new ReturnObject(ReturnNo.OK);
    }

    /**
     * 管理员解禁用户
     * @param shopId 商店id
     * @param id 用户id
     * @param user 用户
     */
    @Audit(departName = "shops")
    @PutMapping("/customers/{id}/release")
    public ReturnObject releaseUser(@PathVariable Long shopId,
                                    @PathVariable Long id,
                                    @LoginUser UserDto user){
        if(!Constants.PLATFORM.equals(shopId)){
            throw  new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE, String.format(ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage(),"顾客", id, shopId));
        }
        customerService.releaseUser(id, user);
        return new ReturnObject(ReturnNo.OK);
    }

}
